package com.onpositive.mediawiki.deserializing;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import com.onpositive.data.IObjectCollection;
import com.onpositive.data.IPropertyConstants;
import com.onpositive.data.IPropertyMetadata;
import com.onpositive.mediawiki.propertyparser.ParsedCategory;
import com.onpositive.mediawiki.propertyparser.ParsedDocument;
import com.onpositive.mediawiki.serializing.DocumentNamePropertyMetadata;
import com.onpositive.units.ParsedProperty;
import com.onpositive.units.ParsedValue;

public class CollectionDeserializer {
	
	private ParsedCategory category ;
	
	public CollectionDeserializer( IObjectCollection<Integer> collection, String url ) {
		
		Collection<IPropertyMetadata> mtdLst = collection.properties() ;
		ArrayList<PropertyDeserializer> pDesList = new ArrayList<PropertyDeserializer>() ;
		
		for( IPropertyMetadata mtd : mtdLst )
			if( mtd != null )
				pDesList.add( new PropertyDeserializer(mtd, collection)) ;
		
		this.category = new ParsedCategory("") ;
		for( PropertyDeserializer pDes : pDesList )
			category.addPropertyCharacteristic( pDes.getpChar() ) ;
				
		String picturePath = getPicturePath(url);
		
		File pathFile = new File( picturePath ) ;
		pathFile.mkdirs() ;
		
				
		int size = collection.size() ;		
		for( int i = 0 ; i < size ; i++ )
		{
			Object picture = null ;
			ParsedDocument pDoc = new ParsedDocument() ;
			for( PropertyDeserializer pDes : pDesList )
			{
				if( pDes.getName().equals( "main-picture-metadata") )
				{
					picture = pDes.getValueFromCollection(i) ;
					continue ;
				}
				ParsedValue val = pDes.getValue(i) ;
				if( val == null )
					continue ;
				
				if( pDes.getName().equals( DocumentNamePropertyMetadata.DOCUMENT_NAME_PROPERTY_METADATA_ID ) )
				{
					pDoc.setName( val.getValueAsString() ) ;
					//continue ;
				}				
				
				ParsedProperty prop = new ParsedProperty( pDes.getName() ) ;
				prop.addValue( val ) ;
				pDoc.addProperty( prop ) ;
			}
			String documentName = pDoc.getName();
			if( documentName != null ){
				category.addParsedDocument( pDoc ) ;
				if( picture != null )
					savePicture( picture , picturePath + documentName + ".jpg" ) ;
			}
		}
	}

	public static String getPicturePath(String url) {
		String picturePath ;
		if( !(url.endsWith("\\") || url.endsWith("/")) )
		{
			int ind = url.lastIndexOf('.') ;
			picturePath = url.substring( 0, ind + 1) + "/" ;
		}
		else
			picturePath = url ;
		return picturePath;
	}

	public ParsedCategory getCategory() {
		return category;
	}
	
	private void savePicture( Object obj, String filePath )
	{
		if( !(obj instanceof byte[] ) )
			return ;
		
		byte[] mPic = (byte[]) obj ;
		if( mPic != null && mPic.length != 0 )
		{
			File fl = new File( filePath ) ;
			
			FileOutputStream fs;
			try {
				fl.createNewFile() ;
				
				fs = new FileOutputStream( fl );			
				fs.write(mPic) ;
				fs.close() ;
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
